package com.sky.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishService dishService;

    // 新增套餐
    @Override
    @Transactional
    public void insertSetmeal(SetmealDTO setmealDTO) {
        // 新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insertSetmeal(setmeal);
        // 新增套餐-菜品关联表
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        if(list == null){
            throw new SetmealEnableFailedException("套餐菜品不能为空！");
        }
        Long id = setmeal.getId();
        insertSetmealDish(list, id);
    }

    // 套餐分页查询
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<SetmealVO> list = setmealMapper.pageQuery(dto);
        Page<SetmealVO> p = (Page<SetmealVO>) list;
        return new PageResult(p.getTotal(),p.getResult());
    }

    // 删除套餐
    @Override
    public Integer deleteByIds(List<Long> ids) {
        Integer i = setmealMapper.deleteByIds(ids);
        if(i > 0){
            return 1 ;
        }
        return 0;
    }

    // 根据id查询套餐
    @Override
    @Transactional
    public SetmealVO selectById(Long id) {
        // 查询套餐
        SetmealVO setmealVO = setmealMapper.selectById(id);
        // 查询套餐-菜品关联表
        setmealVO.setSetmealDishes(setmealDishMapper.selectBySetmealId(id));
        return setmealVO;
    }

    // 修改套餐
    @Override
    @Transactional
    public void update(SetmealDTO dto) {
        // 修改套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.update(setmeal);
        // 修改套餐—菜品关联关系表：1. 删除套餐—菜品关联关系 2.新增套餐—菜品关联关系
        List<SetmealDish> list = dto.getSetmealDishes();
        Long id = setmeal.getId();
        setmealDishMapper.deleteBySetmealId(id);
        insertSetmealDish(list,id);
    }

    // 启售停售套餐
    @Override
    public Integer updateByStatus(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        SetmealVO setmealVO = this.selectById(id);
        List<SetmealDish> list = setmealVO.getSetmealDishes();
        for (SetmealDish setmealDish : list) {
            Long dishId = setmealDish.getDishId();
            DishVO dishVO = dishService.selectById(dishId);
            Integer status1 = dishVO.getStatus();
            if(status1 == 0){
                if(status == 1){
                    return 0;
                }
            }
        }
        setmeal.setStatus(status);
        setmealMapper.update(setmeal);
        return 1;
    }

    // 根据分类id查询菜品
    @Override
    public List<Setmeal> selectByCategoryId(Integer categoryId) {
        return setmealMapper.selectByCategoryId(categoryId);
    }

    // 根据套餐id查询包含的菜品
    @Override
    public List<DishItemVO> selectBySetmealId(Integer setmealId) {
        return setmealMapper.selectBySetmealId(setmealId);
    }

    // 根据分类id查询在售菜品
    @Override
    public List<Setmeal> userSelectByCategoryId(Integer categoryId) {
        return setmealMapper.userSelectByCategoryId(categoryId);
    }

    // 新增套餐-菜品方法
    private void insertSetmealDish(List<SetmealDish> list, Long id) {
        if(!CollectionUtils.isEmpty(list)){
            for (SetmealDish setmealDish : list) {
                setmealDish.setSetmealId(id);
            }
            setmealDishMapper.insertSetmealDish(list);
        }
    }


}
